# -*- coding: utf-8 -*- 
# cpp_writer.py
# write cpp file

import os

H_FILE = 'code/datatable.h'

CODE_IMPORT = '#pragma once\n#include <string>\n#include <list>\n#include <sstream>\n#include <vector>\n#include <map>\n#include "cocos2d.h"\n#include "TableReader.h"\n\nUSING_NS_CC;\nusing namespace std;\n\n'

CODE_END = "};\n"
CODE_CLASS_END = "};"
CODE_TAB = "\t"

CODE_TABLE_CLASS = 'class %sTable : public TableReader\n{\npublic:\n\t%sTable() {  }\n\t~%sTable() { purge(); }\n\tbool init(){ init("%s.csv"); return true; };\n\tvoid init(const std::string& filename) { parse(filename, 3); }\n\tvoid purge(){ _%sList.clear(); };\n\tconst %s* get%sByID(int id) { return _%sList[id];};\n\tint getSize() { return _%sList.size();};\nprivate:\n\tvirtual void readline(std::stringstream& _stream);\n\tstd::map<unsigned int, %s*> _%sList;\n};\n\n'
CODE_CLASS_CLASS = 'class %s\n{\npublic:\n\t%s(){};\n\t~%s(){};\n\tvoid readline(std::stringstream& _stream);\n'
CODE_VAR = '\t%s %s;\n'

class hwriter:

    def write_code(self,code):
        file_stream = open(H_FILE,'w')
        file_stream.write(code)
        file_stream.close()
        
    def add_code(self,code):
        file_stream = open(H_FILE,'a')
        file_stream.write(code)
        file_stream.close()
    
    def write_import(self):
        self.write_code(CODE_IMPORT)

    def add_end(self):
        self.add_code(CODE_END)

    def add_table_class(self,tableClassname):
        self.add_code(CODE_TABLE_CLASS%(tableClassname,tableClassname,tableClassname,tableClassname,tableClassname,tableClassname,tableClassname,tableClassname,tableClassname,tableClassname,tableClassname))
        
    def add_class_class(self,classname):
        self.add_code(CODE_CLASS_CLASS%(classname,classname,classname))

    def add_one_var(self,var_type,var_name):
        return CODE_VAR%(var_name,var_type)

    def add_class_var(self,format_list,format_name_list):
        var = ''
        for i in range(0,len(format_list)):
            var += self.add_one_var(format_list[i],format_name_list[i]);
        self.add_code(var)
